//
//  DrawerViewController.m
//  LO_MUSIC
//
//  Created by 赵锐 on 16/2/24.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //添加子视图控制器 //添加view写在了下边的方法中
//    [self addChildViewController:self.leftVC];
//    [self addChildViewController:self.rootVC];
    
    //如果没有手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        self.tap.delegate = self;
        [self.view addGestureRecognizer:self.tap];
    }
}

//设置中间视图控制器
-(void)setRootViewController:(UIViewController *)viewController{
    //如果传进来的viewcontroller是空 直接返回
    if (!viewController) {
        return;
    }
    //设置中间视图控制器
    //如果不删除那么rootVC上的view会越来越多 因为没有释放掉 虽然不影响视觉效果 但是长期下去会占用不必要的内存
    [self.rootVC.view removeFromSuperview];
    self.rootVC = viewController;
    [self addChildViewController:viewController];
    //如果交互开着就证明抽屉是打开的//下边的if语句如果不写点击阅读和音乐按钮就不会有动画效果了 因为每次点击都是一个新的
    if (self.tap.enabled) {
        viewController.view.frame = CGRectMake(200, 0, 375, 667);
    }
    [self.view addSubview:viewController.view];
    
    //关闭抽屉
    [self showRootViewController:nil];
    //设置导航按钮
    [self setNaButton];
    
}
//展示中间控制器视图(关闭抽屉)
-(void)showRootViewController:(UIViewController *)viewController{
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(0, 0, 375, 667);
    }];
    
    //手势关掉
    [self.tap setEnabled:NO];
    //rootView交互打开
    self.rootVC.view.userInteractionEnabled = YES;
    
}
//展示侧滑栏控制器视图(打开抽屉)
-(void)showLeftViewController:(UIViewController *)viewController{
    
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        //插入抽屉的view
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    //动画移动根视图控制器
    [UIView animateWithDuration:0.4 animations:^{
        self.rootVC.view.frame = CGRectMake(200, 0, 375, 667);
    }];
    
    //手势打开
    [self.tap setEnabled:YES];
    //rootview禁止使用
    self.rootVC.view.userInteractionEnabled = NO;

}

//设置左上角按钮
-(void)setNaButton{
    UINavigationController * na = (UINavigationController *)self.rootVC;
    UIViewController * vc = na.viewControllers[0];
    vc.navigationItem.leftBarButtonItem = ({
        UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithTitle:@"抽屉" style:(UIBarButtonItemStyleDone) target:self action:@selector(openMeun)];
        item;
    });
}
//打开抽屉
-(void)openMeun{
    [self showLeftViewController:nil];
    [self.tap setEnabled:YES];
    self.rootVC.view.userInteractionEnabled = NO;
}


//关闭抽屉手势
-(void)tapAction{
    [self showRootViewController:nil];
    [self.tap setEnabled:NO];
}

//tap的delegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:_rootVC.view]);
    }
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
